import { BusinessError } from '@kit.BasicServicesKit';
import bundleManager from '@ohos.bundle.bundleManager';
import featureAbility from '@ohos.ability.featureAbility';
import deviceInfo from '@ohos.deviceInfo';
import Intl from '@ohos.intl';
import i18n from '@ohos.i18n';
import { getTaskLogger } from '../logger/Logger';

let logger = getTaskLogger('SystemInfoUtil')

export class SystemInfoUtil {
  static getVersionCode(): number {
    try {
      let bundleFlags = bundleManager.BundleFlag.GET_BUNDLE_INFO_WITH_REQUESTED_PERMISSION;
      let a = bundleManager.getBundleInfoForSelfSync(bundleFlags);
      return a.versionCode;
    } catch (err) {
      let message = (err as BusinessError).message;
      // logger.error(0x0000, 'testTag', 'getAppProvisionInfo failed: %{public}s', message);
      return 0;
    }
  }

  static getVersionName(): string {
    try {
      let bundleFlags = bundleManager.BundleFlag.GET_BUNDLE_INFO_WITH_REQUESTED_PERMISSION;
      let a = bundleManager.getBundleInfoForSelfSync(bundleFlags);
      return a.versionName;
    } catch (err) {
      let message = (err as BusinessError).message;
      // logger.error(0x0000, 'testTag', 'getAppProvisionInfo failed: %{public}s', message);
      return "";
    }
  }

  /**
   * 设备厂家名称
   * @returns
   */
  static deviceType(): string {
    return deviceInfo.deviceType;
  }

  /**
   * 设备品牌名称
   * @returns
   */
  static brand(): string {
    return deviceInfo.brand;
  }

  /**
   * 外部产品系列
   * @returns
   */
  static marketName(): string {
    return deviceInfo.marketName;
  }

  /**
   * 系统版本
   * @returns
   */
  static osFullName(): string {
    return deviceInfo.osFullName;
  }

  /**
   * 认证型号
   * @returns
   */
  static productModel(): string {
    return deviceInfo.productModel;
  }

  /**
   * 系统语言
   * @returns
   */
  static getSystemLanguage(): string {
    try {
      let systemLanguage: string = i18n.System.getSystemLanguage(); // systemLanguage为当前系统语言
      return systemLanguage;
    } catch (error) {
      let err: BusinessError = error as BusinessError;
      // logger.error(err);
      return "";
    }
  }

  /**
   * app名字
   * @returns
   */
  static getAppName(): string {
    let bundleFlags = bundleManager.BundleFlag.GET_BUNDLE_INFO_WITH_APPLICATION | bundleManager.BundleFlag.GET_BUNDLE_INFO_WITH_METADATA;
    try {
      let bundleInfo = bundleManager.getBundleInfoForSelfSync(bundleFlags);
      return bundleInfo.appInfo.name;
    } catch (err) {
      let message = (err as BusinessError).message;
      // logger.error(0x0000, 'testTag', 'getBundleInfoForSelf failed: %{public}s', message);
      return "";
    }
  }
}
